#include "MonitoringLogin.h"
#include "ui_MonitoringLogin.h"
#include "SnaillTcpClient.h"
MonitoringLogin::MonitoringLogin(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MonitoringLogin),
    userSqlite(MonitoringUserSqlite::getInstance())
{
    ui->setupUi(this);
    timer=new QTimer(this);
    connect(timer, &QTimer::timeout, this,&MonitoringLogin::timerFinish);
//    connect(timer, &QTimer::timeout, this,[=](){
//                SnaillTcpClient* client = SnaillTcpClient::instance();
//                    if (client->connectToServer("127.0.0.1", 6666)) {
//                        qDebug() << "Connected to server1111";
//                    } else {
//                        qDebug() << "Failed to connect to server11111";
//                    }
//            });
//    timer->start(10000);
    //注册
    connect(ui->btnRegister,&QPushButton::clicked,this,[=](){
        emit registerClick();
         LOG_INFO("点击了注册");
    });
    //登录
    connect(ui->btnLog,&QPushButton::clicked,this,[=](){
        LOG_INFO("点击了登录");
        qDebug()<<"aaa";
        timer->start(3000);
        if(!MonitoringTool::isPhoneNumberValid(ui->lineEditUserName->text())){

            timer->start(3000);
            ui->label_2->setText("手机号格式不正确");
            return ;
        }
        qDebug()<<"bbb";
        if(ui->lineEditPassword->text().isEmpty()){
            timer->start(3000);
            ui->label_2->setText("密码不能为空");
            return ;
        }
        LOG_INFO("登录了");
        if (SnaillTcpClient::instance()->sendJsonData(MonitoringTool::getUserJson("login",ui->lineEditUserName->text(),ui->lineEditPassword->text(),"",""))) {
            //  qDebug() << "Login data sent successfully";
        } else {
            ui->label_2->setText("登录失败，请检查网络");
            return ;
        }

        //        if(userSqlite.loginGetData(ui->lineEditUserName->text(),ui->lineEditPassword->text())==QUERY_SUCCESS_USER_NOT_EXISTS){
        //            timer->start(3000);
        //            ui->label_2->setText("用户或者密码错误不存在");
        //            return ;
        //        }
        //        if(userSqlite.loginGetData(ui->lineEditUserName->text(),ui->lineEditPassword->text())==QUERY_FAILED){
        //            timer->start(3000);
        //            ui->label_2->setText("数据库操作失败");
        //            return ;
        //        }
        //        emit loginClick(ui->lineEditUserName->text());
    });
    //忘记密码
    connect(ui->btnForgot,&QPushButton::clicked,this,[=](){
        emit forgotClick();
        LOG_INFO("点击了忘记密码");
    });
    connect(SnaillTcpClient::instance(), &SnaillTcpClient::dataReceived, this, &MonitoringLogin::handleDataReceived);
}
void MonitoringLogin::timerFinish()
{
    ui->label_2->clear();

}
void MonitoringLogin::handleDataReceived(const QByteArray &data)
{
    qDebug()<<"login==+++=="<<data;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(data);
    QJsonObject jsonObj = jsonDoc.object();
    QString action = jsonObj["action"].toString();
    qDebug()<<"action==+++=="<<action;
    if(action=="login"){
        qDebug()<<"action=="<<action;
        QString coded = jsonObj["code"].toString();
        int code = coded.toInt();
        if(code==QUERY_SUCCESS_USER_NOT_EXISTS){
            timer->start(3000);
            ui->label_2->setText("用户或者密码错误不存在");
            return ;

        }
        if(code==QUERY_FAILED){
            timer->start(3000);
            ui->label_2->setText("数据库操作失败");
            return ;
        }
        emit loginClick(ui->lineEditUserName->text());
    }


}
MonitoringLogin::~MonitoringLogin()
{
    delete ui;
    delete timer;
}
